package com.framework.modules.dataview.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.framework.common.validator.ValidatorUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.framework.modules.dataview.entity.ViewLiveRoomListEntity;
import com.framework.modules.dataview.service.ViewLiveRoomListService;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;


/**
 * VIEW
 *
 * @author MIT
 * @email litmiss@gmail.com
 * @date 2020-12-16 09:50:48
 */
@RestController
@RequestMapping("dataview/viewliveroomlist")
@Api(tags = "ViewLiveRoomListController")
public class ViewLiveRoomListController {
    @Autowired
    private ViewLiveRoomListService viewLiveRoomListService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dataview:viewliveroomlist:list")
    @ApiOperation("list")
    public R list(@RequestParam Map<String, Object> params) {
        String nickname=(String)params.get("nickname");
        String state=(String)params.get("state");
        String liveType=(String)params.get("live_type");
        if(StringUtils.isBlank(nickname)){
            params.remove("nickname");
        }
        if(StringUtils.isBlank(state)){
            params.remove("state");
        }
        if("today_live".equals(liveType)){
            long startTime= DateUtil.beginOfDay(new Date()).getTime()/1000;
            params.put("startTime",startTime);
        }

        PageUtils page = viewLiveRoomListService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/query_data_list")
    @RequiresPermissions("dataview:viewliveroomlist:list")
    @ApiOperation("query_data_list")
    public R query_data_list(@RequestParam Map<String, Object> params) {
        params.put("state","1");
        params.put("1","1");
        params.put("page", params.getOrDefault("page","1"));
        params.put("limit",params.getOrDefault("limit","12"));
        PageUtils page = viewLiveRoomListService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{uid}")
    @RequiresPermissions("dataview:viewliveroomlist:info")
    @ApiOperation("info")
    public R info(@PathVariable("uid") Integer uid) {
        ViewLiveRoomListEntity viewLiveRoomList = viewLiveRoomListService.selectById(uid);

        return R.ok().put("viewLiveRoomList", viewLiveRoomList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dataview:viewliveroomlist:save")
    @ApiOperation("save")
    public R save(@RequestBody ViewLiveRoomListEntity viewLiveRoomList) {
        viewLiveRoomListService.insert(viewLiveRoomList);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dataview:viewliveroomlist:update")
    @ApiOperation("update")
    public R update(@RequestBody ViewLiveRoomListEntity viewLiveRoomList) {
        ValidatorUtils.validateEntity(viewLiveRoomList);
        viewLiveRoomListService.updateAllColumnById(viewLiveRoomList);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dataview:viewliveroomlist:delete")
    @ApiOperation("delete")
    public R delete(@RequestBody Integer[] uids) {
        viewLiveRoomListService.deleteBatchIds(Arrays.asList(uids));

        return R.ok();
    }

}
